//
//  Y2DevicePermission.m
//  Y2Common
//
//  Created by Mac on 2021/6/1.
//

#import "Y2DevicePermission.h"
#import "Y2DeviceInfo.h"
#import <AdSupport/AdSupport.h>
#import <AVFoundation/AVFoundation.h>
#import <AppTrackingTransparency/ATTrackingManager.h>
#import <CoreServices/CoreServices.h>

@implementation Y2DevicePermission

+ (void) requestIDFA:(void(^)(NSString *idfa))result {
    if (@available(iOS 14, *)) {
        if (ATTrackingManager.trackingAuthorizationStatus == ATTrackingManagerAuthorizationStatusNotDetermined) {
            [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
                if (result) result([Y2DeviceInfo getIDFA]);
            }];
        }else{
            if (result) result([Y2DeviceInfo getIDFA]);
        }
    } else {
        if (result) result([Y2DeviceInfo getIDFA]);
    }
}

// 判断应用是否有用户授予的权限
+ (BOOL) hasAuthorityMedia:(NSString *)mediaType {
    //读取设备授权状态
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    return authStatus != AVAuthorizationStatusRestricted && authStatus != AVAuthorizationStatusDenied;
}

#pragma mark - 摄像头相关
// 判断设备是否有摄像头
+ (BOOL) isCameraAvailable {
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}

// 前置的摄像头是否可用
+ (BOOL) isFrontCameraAvailable {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}

// 后置的摄像头是否可用
+ (BOOL) isRearCameraAvailable {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

// 检查摄像头是否支持录像
+ (BOOL) doesCameraSupportShootingVideos {
    return [Y2DevicePermission cameraSupportsMedia:(NSString *)kUTTypeMovie
                                        sourceType:UIImagePickerControllerSourceTypeCamera];
}

// 检查摄像头是否支持拍照
+ (BOOL) doesCameraSupportTakingPhotos {
    return [Y2DevicePermission cameraSupportsMedia:(NSString *)kUTTypeImage
                                        sourceType:UIImagePickerControllerSourceTypeCamera];
}


+ (BOOL) isPhotoLibraryAvailable {
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
}


+ (BOOL) canUserPickVideosFromAlbum {
    return [Y2DevicePermission cameraSupportsMedia:(NSString *)kUTTypeMovie
                                        sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}


+ (BOOL) canUserPickPhotosFromAlbum {
    return [Y2DevicePermission cameraSupportsMedia:(NSString *)kUTTypeImage
                                        sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

// 判断是否支持某种多媒体类型：拍照，视频
+ (BOOL) cameraSupportsMedia:(NSString *)mediaType sourceType:(UIImagePickerControllerSourceType)sourceType {
    __block BOOL result = NO;
    if ([mediaType length] == 0) {
        return NO;
    }
    NSArray *availableMediaTypes =[UIImagePickerController availableMediaTypesForSourceType:sourceType];
    [availableMediaTypes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL*stop) {
        NSString *type = (NSString *)obj;
        if ([type isEqualToString:mediaType]){
            result = YES;
            *stop= YES;
        }
        
    }];
    return result;
}


@end
